<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Packageoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('packageoption');
	}
	public function ajax_packageoption_get(){
		$packageoptionEntity =  new CI_Package_option_entity();
		$packageoption = $packageoptionEntity->getPackaggOptionByCode($_REQUEST['code']);
		echo json_encode($packageoption);
	}
	public function ajax_packageoption_list(){
		$packageoptionEntity =  new CI_Package_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$packageoptionList = $packageoptionEntity->getPackageOptionByLanguage($defaultLanguageId);
		$data['packageoptionList'] = $packageoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/packageoptions/index', $data);
	}
	public function ajax_packageoption_save(){
		$packageoptionNameList = $_REQUEST['packageoption'];
		$packageoptionPriceList = $_REQUEST['packageprice'];
		if($this->input->post('apply_period')){
			$packageoptionApplyPeriod = $_REQUEST['apply_period'];
		}else{
			$packageoptionApplyPeriod = 0;
		}
		$packageoptionNameDefault = '';
		foreach ($packageoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$packageoptionNameDefault = $value;
				break;
			}
		}
		$packageoptionPriceDefault = 0;
		foreach ($packageoptionPriceList as $key => $value) {
			if($value != null && $value != ""){
				$packageoptionPriceDefault = $value;
				break;
			}
		}
		
		$result = array();
		if($packageoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_packageoption_require');
		}else{
			$code = 0;
			foreach ($packageoptionNameList as $key => $value) {
				$packageoptionEntity =  new CI_Package_option_entity();
				if($value != null && $value != ''){
					$packageoptionEntity->setPackage($value);
				}else{
					$packageoptionEntity->setPackage($packageoptionNameDefault);
				}
				if($packageoptionPriceList[$key] != null){
					$packageoptionEntity->setPrice($packageoptionPriceList[$key]);
				}else{
					$packageoptionEntity->setPrice($packageoptionPriceDefault);
				}
				
				$packageoptionEntity->setApplyPeriod($packageoptionApplyPeriod);
								
				$packageoptionEntity->setLanguageId($key);
				$packageoptionEntity->setCode($code);
				$id = $packageoptionEntity->save();
				if($code == 0){
					$code = $id;
					$packageoptionEntity->setId($id);
					$packageoptionEntity->setCode($code);
					$packageoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_packageoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_packageoption_update(){
		$packageoptionNameList = $_REQUEST['packageoption'];
		$packageoptionPriceList = $_REQUEST['packageprice'];
		$packageoptionIdList = $_REQUEST['packageoption_id'];
		$packageoptionCode = $_REQUEST['packageoption_code'];
		
		if($this->input->post('apply_period')){
			$packageoptionApplyPeriod = $_REQUEST['apply_period'];
		}else{
			$packageoptionApplyPeriod = 0;
		}
		
		$packageoptionNameDefault = '';
		foreach ($packageoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$packageoptionNameDefault = $value;
				break;
			}
		}
		$packageoptionPriceDefault = 0;
		foreach ($packageoptionPriceList as $key => $value) {
			if($value != null && $value != ""){
				$packageoptionPriceDefault = $value;
				break;
			}
		}
		$result = array();
		if($packageoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_packageoption_require');
		}else{
			foreach ($packageoptionNameList as $key => $value) {
				$packageoptionEntity =  new CI_Package_option_entity();
				if($value != null && $value != ''){
					$packageoptionEntity->setPackage($value);
				}else{
					$packageoptionEntity->setPackage($packageoptionNameDefault);
				}
				if($packageoptionPriceList[$key] != null){
					$packageoptionEntity->setPrice($packageoptionPriceList[$key]);
				}else{
					$packageoptionEntity->setPrice($packageoptionPriceDefault);
				}
				
				$packageoptionEntity->setApplyPeriod($packageoptionApplyPeriod);
				
				$packageoptionEntity->setLanguageId($key);
				$packageoptionEntity->setCode($packageoptionCode);
				$packageoptionEntity->setId($packageoptionIdList[$key]);
				if($packageoptionIdList[$key]){
					$packageoptionEntity->update();
				}else{
					$packageoptionEntity->save();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_packageoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_packageoption_delete(){
		$result = array();
		$packageoptionEntity =  new CI_Package_option_entity();
		$result['result'] = $packageoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_packageoption_delete_success');
		}else{
			$result['message'] = lang('escort_packageoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>